import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.classic.filter.LevelFilter
import ch.qos.logback.classic.filter.ThresholdFilter
import ch.qos.logback.core.ConsoleAppender
import ch.qos.logback.core.rolling.RollingFileAppender
import ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy

import java.nio.charset.Charset

import static ch.qos.logback.classic.Level.ERROR
import static ch.qos.logback.classic.Level.INFO
import static ch.qos.logback.core.spi.FilterReply.ACCEPT
import static ch.qos.logback.core.spi.FilterReply.DENY

def projectName = 'fount4j-generator'

scan('30 seconds')
appender('STDOUT', ConsoleAppender) {
  encoder(PatternLayoutEncoder) {
    pattern = '[%level][%date{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %class.%method\\(%file:%line\\) - %msg%n'
    charset = Charset.forName('UTF-8')
  }
}

appender('ERROR-OUT', RollingFileAppender) {
  file = "D:/logs/${projectName}.error.log"
  encoder(PatternLayoutEncoder) {
    pattern = '[%date{yyyy-MM-dd HH:mm:ss.SSS}][%relative] [%thread] - %msg%n%caller'
    charset = Charset.forName('UTF-8')
  }
  filter(LevelFilter) {
    level = ERROR
    onMatch = ACCEPT
    onMismatch = DENY
  }
  rollingPolicy(TimeBasedRollingPolicy) {
    fileNamePattern = "${projectName}.error.%d{yyyy-MM-dd}.zip"
    maxHistory = 30
  }
}

appender('SUM-OUT', RollingFileAppender) {
  file = "D:/logs/${projectName}.log"
  encoder(PatternLayoutEncoder) {
    pattern = '[%level][%date{yyyy-MM-dd HH:mm:ss.SSS}][%relative] [%thread] - %msg%n%caller'
    charset = Charset.forName('UTF-8')
  }
  filter(ThresholdFilter) {
    level = INFO
  }
  rollingPolicy(TimeBasedRollingPolicy) {
    fileNamePattern = "${projectName}.%d{yyyy-MM-dd}.%i.log"
    timeBasedFileNamingAndTriggeringPolicy(SizeAndTimeBasedFNATP) {
      maxFileSize = '200MB'
    }
  }
}

logger('com.alibaba.druid', INFO)
root(INFO, ['STDOUT', 'ERROR-OUT', 'SUM-OUT'])
